package cn.edu.xmu.javaee.springsecurity.dao;


import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.util.JacksonUtil;
import cn.edu.xmu.javaee.springsecurity.dao.bo.ProductDraft;
import cn.edu.xmu.javaee.springsecurity.util.RequestVariables;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Repository;

import java.util.Locale;

@Repository
@Slf4j
@RequiredArgsConstructor
public abstract class ProductDao {

    private final MessageSource messageSource;

    public ProductDraft insert(ProductDraft draft){
        draft.setId(1L);
        return draft;
    }

    public ProductDraft findById(Long shopId, Long id) throws BusinessException{
        Locale locale = this.getRequestVariables().getLocale();
        if (1L != id) {
            String[] objects = new String[] {messageSource.getMessage("productapplication", null, locale), id.toString()};
            throw new BusinessException(ReturnNo.RESOURCE_ID_NOTEXIST, JacksonUtil.toJson(objects));
        } else{
            if (2L != shopId){
                String[] objects = new String[] {messageSource.getMessage("productapplication", null, locale), id.toString(), shopId.toString()};
                throw new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, JacksonUtil.toJson(objects));
            }else {
                return ProductDraft.builder().id(1L).name("测试1").originalPrice(100L).originPlace("厦门").categoryId(2L).shopId(2L).productId(100L).build();
            }
        }
    }

    public void deleteById(Long id){
        return;
    }

    public void updateById(ProductDraft draft){
        return;
    }

    @Lookup
    protected abstract RequestVariables getRequestVariables();
}
